package gw.com.android.ui.home;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gwtsz.gts2.hx.R;

/**
 * @author jett
 * @since 2018-11-08.
 */
public class NewsPageAdapter extends PagerAdapter {

    // 日历要闻快讯相关
    private static final int TOPNEWS = 0;
    private static final int FLASH = 1;
    private static final int CALENDAR = 2;

    private final Activity mActivity;
    private HomeTopnewsPresenter topnewsPresenter;
    private HomeFlashPresenter flashPresenter;
    private HomeCalendarPresenter calendarPresenter;

    NewsPageAdapter(Activity activity) {
        this.mActivity = activity;
    }

    public void onPageSelected(int position, boolean frozen) {
        if (position == TOPNEWS) {
            if (topnewsPresenter != null) {
                topnewsPresenter.onSelected();
            }
        } else if (position == FLASH) {
            if (flashPresenter != null) {
                flashPresenter.onSelected(frozen);
            }
        } else if (position == CALENDAR) {
            if (calendarPresenter != null) {
                calendarPresenter.onSelected(frozen);
            }
        }
    }

    public void onRefresh(int position) {
        if (position == TOPNEWS) {
            if (topnewsPresenter != null) {
                topnewsPresenter.onRefresh();
            }
        } else if (position == FLASH) {
            if (flashPresenter != null) {
                flashPresenter.onRefresh();
            }
        } else if (position == CALENDAR) {
            if (calendarPresenter != null) {
                calendarPresenter.onRefresh();
            }
        }
    }

    public void onRefreshAll() {
        if (topnewsPresenter != null) {
            topnewsPresenter.onRefresh();
        }
        if (flashPresenter != null) {
            flashPresenter.onRefresh();
        }
        if (calendarPresenter != null) {
            calendarPresenter.onRefresh();
        }
    }

    public void setFrozen(boolean frozen) {
        if (topnewsPresenter != null) {
            topnewsPresenter.setFrozen(frozen);
        }
        if (flashPresenter != null) {
            flashPresenter.setFrozen(frozen);
        }
        if (calendarPresenter != null) {
            calendarPresenter.setFrozen(frozen);
        }
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view;
        switch (position) {
            case TOPNEWS:
                view = LayoutInflater.from(container.getContext()).inflate(R.layout.view_home_topnews, container, false);
                topnewsPresenter = new HomeTopnewsPresenter(mActivity, view);
                break;
            case FLASH:
                view = LayoutInflater.from(container.getContext()).inflate(R.layout.view_home_flash, container, false);
                flashPresenter = new HomeFlashPresenter(mActivity, view);
                break;
            case CALENDAR:
                view = LayoutInflater.from(container.getContext()).inflate(R.layout.view_home_calendar, container, false);
                calendarPresenter = new HomeCalendarPresenter(mActivity, view);
                break;
            default:
                return super.instantiateItem(container, position);
        }
        container.addView(view);
        return view;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case TOPNEWS:
                return mActivity.getString(R.string.home_trade_news_topnews);
            case FLASH:
                return mActivity.getString(R.string.home_trade_news_flash);
            case CALENDAR:
                return mActivity.getString(R.string.home_trade_news_calendar);
        }
        return super.getPageTitle(position);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

}
